package actions;

import net.sf.json.JSONObject;
import utils.DBSelect;
import utils.DBSelectInt;
import utils.JSONResponse;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PersonalDataAction extends CMActionSupport{
	private String userId;
	private String stuNum;
	private String userName;
	private String sex;
	private String level;
	private String phone;
	private String newPass;
	private String password;

	public String show() {
		JSONObject result = new JSONObject();
		final List<JSONObject> jsonList = new ArrayList<>();
		String sql = "select * from user where id = '"+userId+"'";
		new DBSelect(sql).exec(new DBSelectInt() {

			@Override
			public boolean deal(ResultSet rs) throws SQLException {
				while (rs.next()) {
					JSONObject tmpJson = new JSONObject();
					tmpJson.put("stuNum", rs.getString("id"));
					tmpJson.put("userName", rs.getString("name"));
					tmpJson.put("sex", rs.getString("sex"));
					tmpJson.put("phone", rs.getString("phone"));
					int role = rs.getInt("level");
					String jurisdiction;
					switch (role) {
						case 1:
							jurisdiction = "普通用户";
							break;
						case 2:
							jurisdiction = "普通管理员";
							break;
						case 3:
							jurisdiction = "高级管理员";
							break;
						default:
							jurisdiction = "读取出错";
					}
					tmpJson.put("level", jurisdiction);
					jsonList.add(tmpJson);
				}
				return false;
			}
		});
		result.put("data",jsonList);
		JSONResponse.returnJsonToResponse(result);
		return NONE;
	}

	public String modifyPwd(){
        final JSONObject result=new JSONObject();
		String sql = "select modify_password('"+userId+"', '"+password+"', '"+newPass+"', '6666')";
		new DBSelect(sql).exec(new DBSelectInt() {

			@Override
			public boolean deal(ResultSet rs) throws SQLException {
                result.put("success","0");
                if(rs.getInt(1)== 1)
                	result.put("success","1");
				return false;
			}
		});
        JSONResponse.returnJsonToResponse(result);
		return NONE;
	}

	public String getStuNum() {
		return stuNum;
	}

	public void setStuNum(String stuNum) {
		this.stuNum = stuNum;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getNewPass() {
		return newPass;
	}

	public void setNewPass(String newPass) {
		this.newPass = newPass;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
}
